package cn.itcast.solr;

import cn.itcast.pojo.Heima56;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.io.IOException;

@SuppressWarnings("all")
public class SolrIndexCreateTest {
    @Test
    public void testCreateIndex() throws IOException, SolrServerException {
        //访问solr服务，直达核心
        SolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr/collection1");
        //创建文档对象
        SolrInputDocument document=new SolrInputDocument();
        //给文档对象中添加字段
        document.addField("id","10010");
        document.addField("title","使用slorj给solr中加入索引数据");
        document.addField("price",123456);
        //把文档对象交给solr，保存
        solrServer.add(document);
        solrServer.commit();
    }
    @Test
    public void testUpdateIndex() throws IOException, SolrServerException {
        //访问solr服务，直达核心
        SolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr/collection1");
        //创建文档对象
        SolrInputDocument document=new SolrInputDocument();
        //给文档对象中添加字段
        document.addField("id","10010");
        document.addField("title","测试使用slorj给solr修改索引数据");
        document.addField("price",123456);
        //把文档对象交给solr，保存
        solrServer.add(document);
        solrServer.commit();
    }
    @Test
    public void testCreateBeanIndex() throws IOException, SolrServerException {
        //访问solr服务，直达核心
        SolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr/collection1");
        //创建文档对象
        Heima56 heima56=new Heima56();
        heima56.setId("10086");
        heima56.setTitle("heima56 666");
        heima56.setPrice(12345L);
        //把文档对象交给solr，保存
        solrServer.addBean(heima56);
        solrServer.commit();
    }
}
